﻿using BGYMqttServer.Handlers;
using Core;
using Core.Web;
using Microsoft.AspNetCore.Mvc;
using MQTTnet.Server;
using MQTTnet.Server.Status;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace BGYMqttServer.Controllers.Api.v1
{
    /// <summary>
    /// mqtt控制器
    /// </summary>
    public class MqttClientsController : ApiController
    {
        /// <summary>
        /// 获取所有客户端状态
        /// </summary>
        /// <param name="mqttServer"></param>
        /// <returns></returns>
        [HttpGet("ClientStatus")]
        public async Task<ApiResult<IMqttClientStatus[]>> GetClientStatusAsync([FromServices] IMqttServer mqttServer)
        {
            var status = await mqttServer.GetClientStatusAsync();
            return status.ToArray();
        }

        /// <summary>
        /// 获取客户端状态
        /// </summary>
        /// <param name="mqttServer"></param>
        /// <param name="clientId"></param>
        /// <returns></returns>
        [HttpGet("{clientId}/ClientStatus")]
        public async Task<ApiResult<IMqttClientStatus>> GetClientStatusAsync([FromServices] IMqttServer mqttServer, string clientId)
        {
            var status = await mqttServer.GetClientStatusAsync();
            var result = status.Where(item => item.ClientId.EqualsIgnoreCase(clientId)).FirstOrDefault();
            return ApiResult.NoError(result);
        }

        /// <summary>
        /// 获取所有会话状态
        /// </summary>
        /// <param name="mqttServer"></param>
        /// <returns></returns>
        [HttpGet("SessionStatus")]
        public async Task<ApiResult<IMqttSessionStatus[]>> GetSessionStatusAsync([FromServices] IMqttServer mqttServer)
        {
            var status = await mqttServer.GetSessionStatusAsync();
            return status.ToArray();
        }

        /// <summary>
        /// 获取会话状态
        /// </summary>
        /// <param name="mqttServer"></param>
        /// <param name="clientId"></param>
        /// <returns></returns>
        [HttpGet("{clientId}/SessionStatus")]
        public async Task<ApiResult<IMqttSessionStatus>> GetSessionStatusAsync([FromServices] IMqttServer mqttServer, string clientId)
        {
            var status = await mqttServer.GetSessionStatusAsync();
            var result = status.Where(item => item.ClientId.EqualsIgnoreCase(clientId)).FirstOrDefault();
            return ApiResult.NoError(result);
        }

        /// <summary>
        /// 获取所有订阅信息
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        [HttpGet("Subscriptions")]
        public ApiResult<MqttSubscription[]> GetSubscriptions([FromServices] MqttSubscriptionProvider provider)
        {
            return provider.GetSubscriptions().ToArray();
        }
        
        /// <summary>
        /// 获取订阅信息
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="clientId"></param>
        /// <returns></returns>
        [HttpGet("{clientId}/Subscriptions")]
        public ApiResult<MqttSubscription> GetSubscriptions([FromServices] MqttSubscriptionProvider provider, string clientId)
        {
            return provider
                .GetSubscriptions()
                .Where(item => item.ClientId.EqualsIgnoreCase(clientId))
                .FirstOrDefault();
        }
    }
}